﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using ASPNETVN.PORTAL.Components.Helpers;

namespace CUSTOMS.Components
{
    public class DanhMuc
    {
        public static string GetTenDoanhNghiep(string maDoanhNghiep)
        {
            string result = string.Empty;
            const string commandText = "SELECT Ten_DV AS Ten FROM SDONVI WHERE Ma_DV = @Ma";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase("SLXNK");
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(commandText);
            db.AddInParameter(dbCommand, "@Ma", SqlDbType.Char, maDoanhNghiep);
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                result = reader.GetString(0);
            }
            reader.Close();
            return FontHelper.TCVN2Unicode(result);
        }

        public static string GetTenDonViHaiQuan(string maHaiquan)
        {
            string result = string.Empty;
            const string commandText = "SELECT Ten_HQ AS Ten FROM SHAIQUAN WHERE Ma_HQ = @Ma";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase("SLXNK");
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(commandText);
            db.AddInParameter(dbCommand, "@Ma", SqlDbType.Char, maHaiquan);
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                result = reader.GetString(0);
            }
            reader.Close();
            return FontHelper.TCVN2Unicode(result);
        }

        public static IDataReader GetDonViHaiQuan(bool includedEmpty)
        {
            string commandText = "SELECT '' AS Ma, N'[Tất cả]' AS Ten UNION SELECT RTRIM(Ma) AS Ma, Ten FROM t_HAIQUAN_DonViHaiQuan";
            if (includedEmpty == false)
            {
                commandText = "SELECT RTRIM(Ma) AS Ma, Ten FROM t_HAIQUAN_DonViHaiQuan";
            }
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            return db.ExecuteReader(CommandType.Text, commandText);
        }

        public static string GetTenLoaiHinhMauDich(string maLoaiHinh)
        {
            string result = string.Empty;
            const string commandText = "SELECT Ten_LH AS Ten FROM SLHINHMD WHERE Ma_LH = @Ma";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase("SLXNK");
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(commandText);
            db.AddInParameter(dbCommand, "@Ma", SqlDbType.Char, maLoaiHinh);
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                result = reader.GetString(0);
            }
            reader.Close();
            return FontHelper.TCVN2Unicode(result);
        }

        public static string GetMaLoaiHinhMauDich(string tenVietTat)
        {
            string result = string.Empty;
            const string commandText = "SELECT Ma_LH AS Ma FROM SLHINHMD WHERE Ten_VT = @TenVietTat";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase("SLXNK");
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(commandText);
            db.AddInParameter(dbCommand, "@TenVietTat", SqlDbType.Char, tenVietTat);
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                result = reader.GetString(0);
            }
            reader.Close();
            return result;
        }

        public static IDataReader GetLoaiHinhMauDich()
        {
            const string commandText = "SELECT '' AS Ma, N'[Tất cả]' AS Ten UNION SELECT RTRIM(TenVietTat) AS Ma, Ten FROM t_HAIQUAN_LoaiHinhMauDich ORDER BY Ten";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            return db.ExecuteReader(CommandType.Text, commandText);
        }
    }
}
